package com.bit.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bit.blog.common.exception.BlogException;
import com.bit.blog.common.utils.JwtUtils;
import com.bit.blog.common.utils.SecuretyUtils;
import com.bit.blog.constant.Constants;
import com.bit.blog.mapper.BlogInfoMapper;
import com.bit.blog.mapper.UserInfoMapper;
import com.bit.blog.pojo.dataobject.BlogInfo;
import com.bit.blog.pojo.dataobject.UserInfo;
import com.bit.blog.pojo.request.UserLoginRequest;
import com.bit.blog.pojo.response.UserInfoResponse;
import com.bit.blog.pojo.response.UserLoginResponse;
import com.bit.blog.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlogInfoMapper blogInfoMapper;
    @Override
    public UserLoginResponse getUser(UserLoginRequest userLoginRequest) {
        UserInfo userInfo = userInfoMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserName,userLoginRequest.getUserName())
                        .eq(UserInfo::getDeleteFlag, Constants.NOT_DELETE));
        if(userInfo == null || userInfo.getId() <= 0){
            throw new BlogException("用户不存在");
        }
        if(!SecuretyUtils.verify(userInfo.getPassword(), userLoginRequest.getPassword())){
            throw new BlogException("用户输入密码不正确");
        }
        Map<String, Object> cliams = new HashMap<>();
        cliams.put("id",userInfo.getId());
        cliams.put("username",userInfo.getUserName());
        String token = JwtUtils.genTolen(cliams);

        return new UserLoginResponse(userInfo.getId(), token);
    }

    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        UserInfoResponse userInfoResponse = new UserInfoResponse();
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo :: getId, userId)
                .eq(UserInfo :: getDeleteFlag, Constants.NOT_DELETE));
        BeanUtils.copyProperties(userInfo, userInfoResponse);
        return userInfoResponse;
    }

    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
        BlogInfo blogInfo = blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId)
                .eq(BlogInfo :: getDeleteFlag, Constants.NOT_DELETE));
        UserInfoResponse userInfoResponse = new UserInfoResponse();
        UserInfo userInfo = userInfoMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo :: getId, blogInfo.getUserId())
                        .eq(UserInfo :: getDeleteFlag, Constants.NOT_DELETE));
        BeanUtils.copyProperties(userInfo, userInfoResponse);
        return userInfoResponse;
    }
}
